<?php

namespace App\Http\Controllers\Api\V1\Common;

use App\Enums\CommonEnum;
use App\Enums\RedisKeyEnum;
use App\Services\SmsService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Http\Requests\Api\V1\Common\Sms\SendRequest;

class SmsController extends Controller
{
    public function __construct()
    {
        if (request()->has('method') && !in_array(request()->input('method'),['login','register'])){
            $this->middleware('jwt.token')
                ->only('send');
        }
    }

    /**
     * @Notes:发送短信
     *
     * @param SendRequest $request
     * @param SmsService $service
     * @return mixed
     * @Author: smile
     * @Date: 2021/4/23
     * @Time: 11:54
     */
    public function send(SendRequest $request,SmsService $service)
    {
        $code = custom_random_code(6);

        $result = $service->allow($request->input('method'))
            ->number($request->input('phone'))
            ->send([$code]);

        if ($result['code'] == 200){
            Redis::setex(RedisKeyEnum::SMS_LOGIN_CODE_KEY.$request->input('phone'),CommonEnum::SMS_CODE_TIME,$code);
        }

        return $this->message('短信发送成功');
    }
}
